package com.yanqu.road.server.manger.activity.springdraw;

import com.yanqu.road.dao.impl.activity.springdraw.SpringDrawConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.daomu.enums.DaoMuSyncConfigTypeEnum;
import com.yanqu.road.entity.activity.springdraw.config.SpringDrawConfig;
import com.yanqu.road.entity.activity.springdraw.config.SpringDrawDigRewardConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.daomu.DaoMuActivityModule;
import com.yanqu.road.server.gameplayer.module.activity.springdraw.SpringDrawModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SpringDrawMgr extends TempMgr {

    /**
     * 活动配置Map
     */
    private static Map<Integer, SpringDrawConfig> activityConfigMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 加载活动
     */
    public static void reloadActivityData() {
        getLogger().info("reload SpringDrawMgr start");
        //获取开启中的活动
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.SpringDraw.getValue());
        //创建活动配置Map
        Map<Integer, SpringDrawConfig> springDrawConfigMap = new ConcurrentHashMap<>();
        for (ActivityInfo activityInfo : list) {
            //获取活动config
            Map<String, ActivityConfig> configMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
            //获取活动专属配置
            Map<Integer, SpringDrawDigRewardConfig> digRewardConfigMap = new SpringDrawConfigDaoImpl().getConfig(activityInfo.getActivityId());
            //创建活动config对象
            SpringDrawConfig springDrawConfig = new SpringDrawConfig(activityInfo, configMap, digRewardConfigMap);
            springDrawConfigMap.put(activityInfo.getActivityId(), springDrawConfig);
        }
        activityConfigMap = springDrawConfigMap;
        //在线玩家,同步下活动数据
        List<GamePlayer> gamePlayerList = GamePlayerMgr.getAllOnlinePlayer();
        for (GamePlayer gamePlayer : gamePlayerList) {
            //同步活动配置
            gamePlayer.getModule(SpringDrawModule.class).syncConfig();
            //同步玩家活动数据
            gamePlayer.getModule(SpringDrawModule.class).syncUserDataMsg();
        }
        getLogger().info("reload SpringDrawMgr end");
    }

    /**
     * 获取活动配置
     * @param activityId
     * @return
     */
    public static SpringDrawConfig getActivityConfig(int activityId) {
        return activityConfigMap.get(activityId);
    }

    /**
     * 获取活动配置Map
     * @return
     */
    public static Map<Integer, SpringDrawConfig> getActivityConfigMap() {
        return activityConfigMap;
    }
}
